package net.samsb.response;

import android.util.Log;

import net.samsb.log.LogUtils;
import net.samsb.request.execute.BaseExecute;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static net.samsb.activity.Constants.ERROR;
import static net.samsb.activity.Constants.HOME_GET_AVATAR_IMG_UI;
import static net.samsb.activity.Constants.HOME_GET_DOOR_LIST;
import static net.samsb.activity.Constants.HOME_GET_WEATHER_IMG_UI;

/**
 * Created by Destiny_hao on 2018/3/24.
 */

public class HttpResponseListener implements Callback {

    private int TYPE = -1;
    private int arg1 = 0;

    private BaseExecute baseExecute;

    public HttpResponseListener(int type, int arg1) {
        this.TYPE = type;
        this.arg1 = arg1;
        this.baseExecute = new BaseExecute();
    }

    @Override
    public void onResponse(Call call, Response response) {

        if (response.isSuccessful()) {
            try {
                LogUtils.e("响应数据：", "onResponse response=" + response);

                //天气图片
                if (TYPE == HOME_GET_WEATHER_IMG_UI) {
                    baseExecute.getPictureExecute().onSucceed(HOME_GET_WEATHER_IMG_UI, response, arg1);
                }

                if (TYPE == HOME_GET_AVATAR_IMG_UI) {
                    baseExecute.getPictureExecute().onSucceed(HOME_GET_AVATAR_IMG_UI, response, arg1);
                }

                if (TYPE == HOME_GET_DOOR_LIST) {
                    //Log.e("doorLIst：", "onResponse responseStr=" + response.body().string());
                    baseExecute.getDoorExecute().onSucceed(HOME_GET_DOOR_LIST, response, 200);
                }

                // Log.e("请求数据结果：", "onResponse responseStr=" + response.body().bytes().toString());

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else {

            if (TYPE == HOME_GET_DOOR_LIST) {
                baseExecute.getDoorExecute().onFailed(ERROR, "服务器IP不可用");
            }

            if (TYPE == HOME_GET_WEATHER_IMG_UI || TYPE == HOME_GET_AVATAR_IMG_UI) {
                baseExecute.getPictureExecute().onFailed(ERROR, "请求错误，请检查网络是否连接");
            }
        }
    }

    @Override
    public void onFailure(Call call, IOException e) {

        LogUtils.e("httpResponseListenerError", e.getMessage());

        if (e instanceof SocketTimeoutException) {
            //判断超时异常
            LogUtils.e("超时异常>>>>>>>>>", e.getMessage());
        }
        if (e instanceof ConnectException) {
            //判断连接异常
            LogUtils.e("连接异常>>>>>>>>>", e.getMessage());
            baseExecute.getDoorExecute().onFailed(ERROR, "服务器IP不可用,请检查网络是否连接");
            return;
        }

        if (TYPE == HOME_GET_DOOR_LIST) {
            baseExecute.getDoorExecute().onFailed(ERROR, "服务器IP不可用");
        }

        if (TYPE == HOME_GET_WEATHER_IMG_UI || TYPE == HOME_GET_AVATAR_IMG_UI) {
            baseExecute.getPictureExecute().onFailed(ERROR, "请求错误，请检查网络是否连接");
        }
    }
}
